package cn.eeepay.framework.dao;

import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.org.PolicyMTime;
import cn.eeepay.framework.org.PolicyQuotaInfo;
import cn.eeepay.framework.org.PolicyTarget;
import cn.eeepay.framework.org.PolicyTargetDetail;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Map;

public interface PolicyTargetDao {

    @SelectProvider(type= PolicyTargetDao.SqlProvider.class, method="list")
    @ResultType(PolicyTarget.class)
    List<PolicyTarget> list(@Param("target") PolicyTarget target, @Param("page") Page<PolicyTarget> page);

    @Select("SELECT * FROM policy_m_time")
    List<PolicyMTime> selectMTimes();

    @Select("SELECT * FROM policy_quota_info WHERE effective_time <= NOW() ORDER BY effective_time DESC")
    List<PolicyQuotaInfo> selectHisQuotas(@Param("page")Page<PolicyQuotaInfo> page);

    @Select("SELECT * FROM policy_quota_info WHERE effective_time > NOW() ORDER BY effective_time DESC")
    List<PolicyQuotaInfo> selectNewQuotas();

    @Delete("DELETE FROM policy_m_time")
    int deleteAll();

    @Insert("<script>"  +
            "INSERT INTO policy_m_time(m_time,float_status,operator) "
            + "VALUES <foreach collection=\"list\" item=\"policyMTime\" index=\"index\" separator=\",\">" +
            "(#{policyMTime.mTime},#{policyMTime.floatStatus},#{policyMTime.operator})" +
            " </foreach>"
            + "</script>")
    int batchInsert(List<PolicyMTime> mTimes);

    @Insert("INSERT INTO policy_m_time(m_time,float_status,operator) VALUES(#{m.mTime},#{m.floatStatus},#{m.operator})")
    int saveMTime(@Param("m") PolicyMTime mTime);

    @Insert("INSERT INTO policy_target_record(" +
            "create_date,m_time," +
            "vip_category,vip_category_type,forecast_vip_ratio," +
            "non_category,non_category_type,forecast_non_ratio," +
            "trans_category,trans_category_type,forecast_trans_ratio," +
            "by_source,operator,remark,use_status,source_type,xun_type,holiday_type) VALUES(" +
            "#{target.createDate},#{target.mTime}," +
            "#{target.vipCategory},#{target.vipCategoryType},#{target.forecastVipRatio}," +
            "#{target.nonCategory},#{target.nonCategoryType},#{target.forecastNonRatio}," +
            "#{target.transCategory},#{target.transCategoryType},#{target.forecastTransRatio}," +
            "#{target.bySource},#{target.operator},#{target.remark},'1',1,#{target.xunType},#{target.holidayType})")
    int savePolicyTarget(@Param("target") PolicyTarget target);

    @Update("UPDATE policy_target_record \n" +
            "SET create_date=#{target.createDate},m_time=#{target.mTime},\n" +
            "vip_category=#{target.vipCategory},vip_category_type=#{target.vipCategoryType},forecast_vip_ratio=#{target.forecastVipRatio},\n" +
            "non_category=#{target.nonCategory},non_category_type=#{target.nonCategoryType},forecast_non_ratio=#{target.forecastNonRatio},\n" +
            "trans_category=#{target.transCategory},trans_category_type=#{target.transCategoryType},forecast_trans_ratio=#{target.forecastTransRatio},\n" +
            "by_source=#{target.bySource},operator=#{target.operator},remark=#{target.remark},xun_type=#{target.xunType},holiday_type=#{target.holidayType} " +
            "WHERE id = #{target.id} AND use_status='1'")
    int updatePolicyTarget(@Param("target") PolicyTarget target);


    @Select("SELECT * FROM policy_target_record_detail WHERE policy_target_record_id=#{id}")
    List<PolicyTargetDetail> selectPolicyTargetDetail(@Param("id") BigInteger policyTargetRecordId);

    @Delete("DELETE FROM policy_quota_info WHERE effective_time > NOW()")
    int deleteQuota();

    @Insert("INSERT INTO policy_quota_info(quota_amount,effective_time,operator) VALUES(#{quota.quotaAmount}, #{quota.effectiveTime},#{quota.operator})")
    int saveNewQuota(@Param("quota") PolicyQuotaInfo quota);

    @Select("\n" +
            "SELECT COUNT(1) FROM policy_quota_info WHERE effective_time=#{effectiveTime}")
    int selectQuotaInfoByEffective(@Param("effectiveTime") Date effectiveTime);

    @Select("SELECT COUNT(1) FROM policy_m_time WHERE m_time=#{mTime}")
    int selectMTimeByMTime(@Param("mTime") Date mTime);

    @Select("SELECT COUNT(1) FROM policy_target_record WHERE create_date=#{createDate} AND m_time=#{mTime} AND source_type=1 ")
    int selectPolicyTargetDetailByTime(@Param("createDate") String createDate, @Param("mTime") String mTime);

    @Select("SELECT * FROM policy_target_record WHERE id=#{id}")
    PolicyTarget selectPolicyTarget(@Param("id") BigInteger id);

    @Delete("DELETE FROM policy_target_record WHERE id=#{id} AND create_date > NOW() AND  use_status='1'")
    int deletePolicyTarget(@Param("id") BigInteger id);

    @Select("SELECT COUNT(1) FROM policy_target_record WHERE create_date=#{createDate} AND m_time=#{mTime} AND source_type=1 AND id<>#{id}")
    int selectPolicyTargetDetailByTimeAndId(@Param("createDate") String createDate, @Param("mTime") String mTime, @Param("id") BigInteger id);

    /*@Select("SELECT m_time FROM policy_m_time")
    List<String> selectMTimeList();*/


    class SqlProvider{
        public String list(Map<String, Object> param){
            PolicyTarget target = (PolicyTarget) param.get("target");
            SQL sql = new SQL(){{
                SELECT(" ptr.*");
                FROM(" policy_target_record ptr");
                if(null != target.getVipCategory()){
                    WHERE("ptr.vip_category = #{target.vipCategory}");
                }
                if(null != target.getVipCategoryType()){
                    WHERE("ptr.vip_category_type=#{target.vipCategoryType}");
                }
                if(null != target.getNonCategory()){
                    WHERE("ptr.non_category=#{target.nonCategory}");
                }
                if(null != target.getNonCategoryType()){
                    WHERE("ptr.non_category_type=#{target.nonCategoryType}");
                }
                if(null != target.getTransCategory()){
                    WHERE("ptr.trans_category=#{target.transCategory}");
                }
                if(null != target.getTransCategoryType()){
                    WHERE("ptr.trans_category_type=#{target.transCategoryType}");
                }
                if(StringUtils.isNotBlank(target.getBySource())){
                    WHERE("ptr.by_source like concat(#{target.bySource},'%')");
                }
                if(StringUtils.isNotBlank(target.getStartCreateDate())){
                    WHERE("ptr.create_date >= #{target.startCreateDate}");
                }
                if(StringUtils.isNotBlank(target.getEndCreateDate())){
                    WHERE("ptr.create_date <= #{target.endCreateDate}");
                }
                if(StringUtils.isNotBlank(target.getmTimeStr())){
                    WHERE("ptr.m_time=#{target.mTimeStr}");
                }
                ORDER_BY("ptr.create_date desc,ptr.m_time ASC");
            }};
            return sql.toString();
        }
    }

}
